home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / sccs / s.xsound.C < prev   
Encoding:
Text File  |  1995-06-30  |  3.5 KB  |  186 lines

  1. h14428
  2. s 00006/00000/00160
  3. d D 1.2 95/05/05 08:02:50 tom 2 1
  4. c Port to ODT 3.0
  5. e
  6. s 00160/00000/00000
  7. d D 1.1 95/05/04 23:36:20 tom 1 0
  8. c date and time created 95/05/04 23:36:20 by tom
  9. e
  10. u
  11. U
  12. t
  13. T
  14. I 2
  15.  
  16. E 2
  17. I 1
  18. #ifndef _global_h
  19. #    include "global.h"
  20. #endif
  21.  
  22. // to get: FIONREAD
  23. #include <sys/ioctl.h>
  24. // to get: unlink()
  25. // #include <unistd.h>
  26.  
  27. #include <X11/Xlib.h>
  28.  
  29. #ifdef SOLARIS
  30. #    include <sys/filio.h>
  31. I 2
  32. #endif
  33.  
  34. #ifdef SCO
  35. #include <sys/socket.h>
  36. #include <sys/select.h>
  37. E 2
  38. #endif
  39.  
  40. #define    QUIT    "quit"
  41.  
  42. #define    DBG    0
  43.  
  44. static XKeyboardState    old_keyboard_values;
  45. static Display    *xsound_dpy;
  46. static pid_t pid;
  47. static int pipefd[2];
  48. static int sound_on=0;
  49. static void local_usleep( long time )
  50. {
  51. struct timeval  timeout;
  52. int    nfound;
  53.  
  54.    timeout.tv_sec  = (long)0;
  55.    timeout.tv_usec = (long)time;
  56.  
  57.    nfound=select(0,0,0,0,&timeout);
  58. }
  59.  
  60. static void sound_loop( char *disp_name ) {
  61. XKeyboardControl    values;
  62. char        buffer[80];
  63. int        valid;
  64. int        pitch, percent,duration;
  65. int        pri, old_pri;
  66. int        len;
  67.  
  68.     xsound_dpy = XOpenDisplay(disp_name);
  69.     if (!xsound_dpy) {
  70.         fprintf( stderr, "can't open display\n" );
  71.         exit(-1);
  72.     }
  73.     XGetKeyboardControl(xsound_dpy,&old_keyboard_values);
  74.  
  75.     valid=0;
  76.     do {
  77.         old_pri = -1;
  78.  
  79.         len=read(0,buffer+valid,sizeof(buffer)-valid);
  80.         if (len<=0)        goto exit_sound;
  81. #if (DBG)
  82.         printf( "* at least %d bytes\n", len);
  83.         fflush(stdout);
  84. #endif
  85.  
  86.         do {
  87.             valid += len;
  88.             char    *lf = strchr(buffer,'\n');
  89.  
  90.             while (lf) {
  91.                 *lf = '\0';
  92. #if (DBG)
  93.                 printf( "* %s\n", buffer );
  94.                 fflush(stdout);
  95. #endif
  96.                 if ( !strcmp(buffer,QUIT) )        goto exit_sound;
  97.                 if ( sscanf(buffer,"%d %d %d %d",&pitch,&percent,&duration,&pri)
  98.                                         ==4 ) {
  99.                     if (pri>old_pri) {
  100.                         values.bell_pitch=pitch;
  101.                         values.bell_percent=percent;
  102.                         values.bell_duration=duration;
  103.                         old_pri=pri;
  104.                     }
  105.                 }
  106.                 valid-=(lf-buffer+1);
  107. #if (0)
  108.     //
  109.     // If you got trouble with memmove, than you've got to change this switch
  110.     //
  111.                 memmove(buffer,lf+1,valid);
  112. #else
  113.                 for (int i=0;i<valid;i++)    buffer[i]=lf[i+1];
  114. #endif
  115.  
  116.                 lf = strchr(buffer,'\n');
  117.             }
  118.  
  119.             ioctl(0,FIONREAD,&len);
  120.             if (len)    len=read(0,buffer+valid,sizeof(buffer)-valid);
  121.         } while(len>0);
  122.     
  123. #if (DBG)    
  124.         printf( "* ------------------------\n" );
  125.         fflush(stdout);
  126. #endif
  127.         XChangeKeyboardControl(xsound_dpy,KBBellPercent|KBBellPitch|KBBellDuration,&values);
  128.         XBell(xsound_dpy,values.bell_percent);
  129.         XFlush(xsound_dpy);
  130.         local_usleep(200000);
  131.     }
  132.     while(1);
  133.  
  134. exit_sound:
  135.  
  136. #if (0)
  137.     values.bell_pitch    = old_keyboard_values.bell_pitch;
  138.     values.bell_percent  = old_keyboard_values.bell_percent;
  139.     values.bell_duration = old_keyboard_values.bell_duration;
  140. #else
  141.     values.bell_pitch    = 400;
  142.     values.bell_percent  =  50;
  143.     values.bell_duration = 100;
  144. #endif
  145.     XChangeKeyboardControl(xsound_dpy,KBBellPercent|KBBellPitch|KBBellDuration,&values);
  146.  
  147.     XCloseDisplay( xsound_dpy );
  148.     printf( "* subprocess xsound termiated\n" );
  149.     exit(0);
  150. }
  151.  
  152.  
  153.  
  154. void init_sound( char *disp_name ) {
  155.  
  156.     pipe (pipefd);
  157.  
  158.     pid=fork();
  159.     if (pid == (pid_t)0) {
  160.         close(0);                    // stdin schlieâ–€en        (Sohn)
  161.         dup (pipefd[0]);
  162.         close (pipefd[1]);
  163.         sound_loop(disp_name);
  164.     }
  165.     else {
  166.         close (pipefd[0]);        //                                (Vater)
  167.         sound_on = 1;
  168.     }
  169. }
  170.  
  171. void end_sound() {
  172.     if (!sound_on)        return;
  173.     write( pipefd[1], QUIT "\n", strlen(QUIT)+1 );
  174.     close( pipefd[1] );
  175. }
  176.  
  177.  
  178. void do_sound( int pitch, int percent, int duration, int pri ) {
  179. char buffer[80];
  180.  
  181.     if (!sound_on)        return;
  182.     sprintf( buffer, "%d %d %d %d\n", pitch, percent, duration, pri );
  183.     write( pipefd[1],buffer,strlen(buffer));
  184. }
  185. E 1
  186.